﻿using System;
using System.Collections.Generic;
using System.Device.Location;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Phone.Maps.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Newtonsoft.Json;


namespace RadarGlobal.MapClass
{
    class MapViewMember
    {
        List<CustomPushpin> viewPushpin = new List<CustomPushpin>();

        public void LoadAllGroupUser(Map MapControl,MapPolygon Radar,int UserID)
        {
            if (NetworkInterface.GetIsNetworkAvailable() == false) return;

            try
            {
                mySQLService.RadarServicePortTypeClient client = new mySQLService.RadarServicePortTypeClient();

                client.GetALlLocationInmyGroupCompleted += (s, args) =>
                {
                    if (args == null) return;

                    if (args.Result == null) return;

                    List<List<string>> listdata = JsonConvert.DeserializeObject<List<List<string>>>(args.Result);

                    if (listdata != null)
                    {
                        foreach (CustomPushpin item in viewPushpin)
                        {
                            item.RemovePushpin(MapControl);
                        }
                        viewPushpin.Clear();

                        foreach (List<string> item in listdata)
                        {
                            GeoCoordinate point = new GeoCoordinate(double.Parse(item[1].ToString()), double.Parse(item[2].ToString()));

                            if(Radar.Contains(point))
                            {
                                CustomPushpin member = new CustomPushpin();
                                member.SetImagePushpin2(MapControl);
                                member.ShowPushpin(MapControl);
                                member.UpdateLocation(point);
                                viewPushpin.Add(member);
                            }
                        }
                    }
                };
                client.GetALlLocationInmyGroupAsync(UserID);
            }
            catch (Exception ex)
            {
            }
        }
    }
}
